package view;

import controllers.EditingInformationControllers;
import entity.User;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * @author zjw
 * @date 2020/1/4 9:46
 */
public class EditingInformationInterface {
    private User user;
    private Stage primaryStage;
    private Label accountNumLabel = new Label("账号：");
    private Label mottoLabel = new Label("签名：");
    private Label nameLabel = new Label("昵称：");
    private Label ageLabel = new Label("年龄：");
    private Label addressLabel = new Label("家乡：");
    private Label telNumLabel = new Label("电话：");

    private Text idText;
    private TextArea mottoTextArea;
    private TextField nameTextField;
    private TextField ageTextField;
    private TextField addressTextField;
    private TextField telNumTextField;

    private RadioButton manRadioButton;
    private RadioButton womanRadioButton;

    private Button saveButton;
    private Button returnButton;

    private GridPane gridPane = new GridPane();
    private ImageView headPortraitImageView;

    private HBox ageAndGenderHBox;
    private HBox buttonHBox;

    /**
     * 有参构造函数
     * 获得需要进行信息修改的组件，并初始化个组件的信息和位置。
     * @param user 需要进行信息修改的user
     */
    public EditingInformationInterface(User user, Stage primaryStage) {
        // 获取当前正在运行的线程账号
        this.user = user;
        this.primaryStage = primaryStage;
        // 添加头像
        Image headPortraitImage = new Image(getClass().getResourceAsStream("../image/Home.jpg"));
        headPortraitImageView = new ImageView(headPortraitImage);

        accountNumLabel = new Label("账号：");
        mottoLabel = new Label("签名：");
        nameLabel = new Label("昵称：");
        ageLabel = new Label("年龄：");
        addressLabel = new Label("家乡：");
        telNumLabel = new Label("电话：");

        idText = new Text();
        mottoTextArea = new TextArea();
        nameTextField = new TextField();
        ageTextField = new TextField();
        addressTextField = new TextField();
        telNumTextField = new TextField();

        manRadioButton = new RadioButton("Man");
        womanRadioButton = new RadioButton("Woman");

        buttonHBox = new HBox();
        EditingInformationControllers editingInformationControllers = new EditingInformationControllers(user, primaryStage,
                idText, mottoTextArea, nameTextField, ageTextField, addressTextField, telNumTextField, manRadioButton,
                womanRadioButton);
        saveButton = editingInformationControllers.getSaveButton();
        returnButton = editingInformationControllers.getReturnButton();
    }

    /**
     * 编辑界面启动方法
     * 按钮的动作信息，以及顶层容器的一些参数设置，并展示该编辑页面
     * @throws Exception
     */
    public void start() throws Exception {
        // 个人信息填充进各个编辑框
        fillInEditBoxWithPersonalInformation();
        initInterface();

        Scene scene = new Scene(gridPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(350);
        primaryStage.setHeight(500);
        primaryStage.setTitle("编辑信息");
        // 添加图标
        primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("../image/Home.jpg")));
        primaryStage.show();
    }

    /**
     * 整体布局编辑界面的各组件的位置
     */
    private void initInterface() {
        // 初始化mottoTextArea框大小和ageTextField框大小,并设置自动换行
        mottoTextArea.setPrefWidth(100);
        mottoTextArea.setPrefHeight(50);
        mottoTextArea.setWrapText(true);
        ageTextField.setPrefWidth(40);

        // 将两个单选按钮放到同一个切换组中，让他们属于同一个事件状态
        ToggleGroup genderToggleGroup = new ToggleGroup();
        manRadioButton.setToggleGroup(genderToggleGroup);
        womanRadioButton.setToggleGroup(genderToggleGroup);
        ageAndGenderHBox = new HBox();
        ageAndGenderHBox.setAlignment(Pos.TOP_LEFT);
        ageAndGenderHBox.setSpacing(3);
        ageAndGenderHBox.getChildren().addAll(ageTextField, manRadioButton, womanRadioButton);

        // 头像位置
        headPortraitImageView.setFitWidth(100);
        headPortraitImageView.setFitHeight(100);

        // 按键布局，使用水平布局
        buttonHBox.setAlignment(Pos.TOP_RIGHT);
        buttonHBox.setSpacing(30);
        buttonHBox.getChildren().addAll(saveButton, returnButton);

        gridPane.setAlignment(Pos.CENTER);
        gridPane.setVgap(20);
        gridPane.setHgap(20);
        gridPane.setPadding(new Insets(20, 20, 20, 20));
        gridPane.add(headPortraitImageView, 1, 0);
        gridPane.add(accountNumLabel, 0, 1);
        gridPane.add(idText, 1, 1);
        gridPane.add(mottoLabel, 0, 2);
        gridPane.add(mottoTextArea, 1, 2);
        gridPane.add(nameLabel, 0, 3);
        gridPane.add(nameTextField, 1, 3);
        gridPane.add(ageLabel, 0, 4);
        gridPane.add(ageAndGenderHBox, 1, 4);
        gridPane.add(addressLabel, 0, 5);
        gridPane.add(addressTextField, 1, 5);
        gridPane.add(telNumLabel, 0, 6);
        gridPane.add(telNumTextField, 1, 6);
        gridPane.add(buttonHBox, 1, 7);
    }

    /**
     * 将user的各信息填充到每各个编辑框中
     */
    private void fillInEditBoxWithPersonalInformation() {
        idText.setText(this.user.getId());
        mottoTextArea.setText(this.user.getMotto());
        nameTextField.setText(this.user.getName());
        ageTextField.setText(this.user.getAge());
        addressTextField.setText(this.user.getAddress());
        telNumTextField.setText(this.user.getTelNum());
        if(user.getGender() == null) {
            manRadioButton.setSelected(true);
        } else if (user.getGender().equals("Man")) {
            manRadioButton.setSelected(true);
        } else {
            womanRadioButton.setSelected(true);
        }
    }


}
